package com.jml.zlhq.app.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.RoundAngleImageView;
import com.loopj.android.http.TextHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

public class UserCollectAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	private Activity activity;
	private int type;

	public UserCollectAdapter(Activity activity, List<JSONObject> itemList, int type) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
		this.type = type;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		convertView = createItem(position, convertView, parent);
		return convertView;
	}
	
	static class ItemHolder {
		public RoundAngleImageView image;
		public TextView name,suju,nomore,cancel;
		public ImageView vip,zan;
	}
	private View createItem(final int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int main_faxian_item = R.layout.collect_item;
			convertView = LayoutInflater.from(appContext).inflate(main_faxian_item, null);
			holder.image = (RoundAngleImageView) convertView.findViewById(R.id.collect_item_image);
			holder.name = (TextView) convertView.findViewById(R.id.collect_item_name);
			holder.suju = (TextView) convertView.findViewById(R.id.collect_item_suju);
			holder.nomore = (TextView) convertView.findViewById(R.id.collect_item_nomore);
			holder.vip = (ImageView) convertView.findViewById(R.id.collect_item_vip);
			holder.zan = (ImageView) convertView.findViewById(R.id.collect_item_zan);
			holder.cancel = (TextView) convertView.findViewById(R.id.collect_item_cancel);
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		final JSONObject json = itemList.get(position);
		try {
			holder.nomore.setVisibility((position==(itemList.size()-1)&&pageEnd)?View.VISIBLE:View.GONE);
			holder.name.setText(BaseTools.getFrom("entry.name", json));
			ImageLoader.getInstance().displayImage(BaseTools.getFrom("entry.cover.image_url", json), holder.image, UIUtil.getImageLoaderDisplayOptions(R.drawable.default140));
			holder.suju.setText(Html.fromHtml("浏览&nbsp;&nbsp;"+BaseTools.getFrom("entry.statistic.CLICK", json)+"&nbsp;&nbsp;&nbsp;&nbsp;收藏&nbsp;&nbsp;"+BaseTools.getFrom("entry.statistic.COLLECT", json)));
			
			if(StringUtil.isEmpty(BaseTools.getFrom("is_paise", json))||Integer.parseInt(BaseTools.getFrom("is_paise", json))==0){
				holder.zan.setVisibility(View.GONE);
			}else{
				holder.zan.setVisibility(View.VISIBLE);
			}
			if(StringUtil.isEmpty(BaseTools.getFrom("is_valid", json))||Integer.parseInt(BaseTools.getFrom("is_valid", json))==0){
				holder.vip.setVisibility(View.GONE);
			}else{
				holder.vip.setVisibility(View.VISIBLE);
			}
			holder.cancel.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Map<String, String> params = new HashMap<String, String>();
					params.put("auth_token", appContext.getLoginUid());
					params.put("target_id", BaseTools.getFrom("target_id", json));
					final Dialog  mProDialog =  UIUtil.showLoadingDialog(activity, "提交中，请稍后...", false);
					HttpService.postByText(type==1?URLs.COLLECT_CANCEL_WORK:URLs.COLLECT_CANCEL_TEAM, params, new TextHttpResponseHandler() {
						
						@Override
						public void onSuccess(int arg0, Header[] arg1, String arg2) {
							mProDialog.dismiss();
							try {
								JSONObject result = new JSONObject(arg2);
								if(result.getInt("status")==0){
									UIUtil.ToastMessage(activity, "取消成功");
									itemList.remove(position);
									notifyDataSetChanged();
								}else{
									UIUtil.ToastMessage(activity, result.getString("message"));
								}
							} catch (JSONException e) {
								e.printStackTrace();
								UIUtil.ToastMessage(activity, "取消失败");
							}
							
						}
						
						@Override
						public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
							mProDialog.dismiss();
							UIUtil.ToastMessage(activity, "取消失败");
						}
					});
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
}
